<?php
/*
 * File:index.php
 * Created:2013-5-17
 * Author:oldjiang
 * 
 */
require_once("common/SearchPath.php");
require_once(SEARCHLIBPATH."db.class.php");
require_once(SEARCHPOPATH."LinkPO.php");
require_once(SEARCHPOPATH."ModPO.php");
require_once(SEARCHPOPATH."HistoryPO.php");
require_once(SEARCHPOPATH."SLogPO.php");
require_once(SEARCHVOPATH."LinkVO.php");
require_once(SEARCHVOPATH."ModVO.php");
require_once(SEARCHVOPATH."HistoryVO.php");
require_once(SEARCHVOPATH."SLogVO.php");
require_once(SEARCHDAOPATH."LinkDAO.php");
require_once(SEARCHDAOPATH."ModDAO.php");
require_once(SEARCHDAOPATH."HistoryDAO.php");
require_once(SEARCHDAOPATH."SLogDAO.php");
require_once(SEARCHBIZPATH."LinkBiz.php");
require_once(SEARCHBIZPATH."ModBiz.php");
require_once(SEARCHBIZPATH."HistoryBiz.php");
require_once(SEARCHBIZPATH."SLogBiz.php");
require_once(SEARCHLIBPATH."array.class.php");
require_once(SEARCHLIBPATH."format.class.php");
require_once(SEARCHLIBPATH."page.class.php");
require_once(SEARCHLIBPATH."httpd.class.php");
require_once(SEARCHLIBPATH."phpanalysis.class.php");
require_once(SEARCHLIBPATH."checkmobile.class.php");
require_once(SEARCHLIBPATH."checkrobot.class.php");
//格式化类
$format = new format();
//手机类
$checkmobile = new checkmobile();
//接受参数处理-关键词
if(isset($_GET['k'])){
	$keyword = $format -> removeHtml($_GET['k']);
}
//接受参数处理-页码
if(isset($_GET['p'])){
	$page_current = $format -> isNumeric($_GET['p']);
}
//接受参数处理-全文查找或标题查找
if(isset($_GET['t'])){
	$type = $format -> removeHtml($_GET['t']);
}
//接受参数处理-模块
if(isset($_GET['m'])){
	$mod = $format -> isNumeric($_GET['m']);
}
//接受参数处理-其他请求处理
if(isset($_GET['act'])){
	$act = $format -> removeHtml($_GET['act']);
	if($act == "clearHistoryWords"){
		if (isset($_COOKIE["s_history"])){
			foreach ($_COOKIE["s_history"] as $name => $value){
				setcookie("s_history[".$name."]", '0',time()-1000);
			}
	  	}
	}
	exit;
}
//没有任何关键词的话，就跳转到搜索首页
if(!isset($keyword)){
	//模块
	$modBiz = new ModBiz();
	$mod_result = $modBiz -> getAll(0);
	if(!isset($_GET['web']) && !isset($_COOKIE["web"]) && $checkmobile->check()){
		include(SEARCHTEMPLATEPATH.MOBILETEMPLATE."search.htm");
	}else{
		setcookie("web", "web", time()+3600);
		include(SEARCHTEMPLATEPATH.TEMPLATE."search.htm");
	}
//搜索相关处理
}else{
	//设置搜索历史
	$search_history = array();
	$max_index=0;
	$isinhistory = false;
	if (isset($_COOKIE["s_history"])){
  		foreach ($_COOKIE["s_history"] as $name => $value){
  			$max_index = $name;
  			if($value!='null' && $value != $keyword)$search_history[] = $value;
    		if($value == $keyword){
    			$isinhistory = true;
    			setcookie("s_history[".$name."]",null , '1');
    		}
    	}
  	}
  	setcookie("s_history[".($max_index+1)."]",$keyword, time()+3600*24*365);
  	$search_history[] = $keyword;
  	$search_history = array_reverse($search_history);
	$linkBiz = new LinkBiz();
	//每页记录数
	$num_per_page = 10;
	//介绍显示文字
	$describe_fonts_num = 180;
	//开始搜索关键词
	$search_result = $linkBiz -> search($keyword,$type,$mod,$page_current,$num_per_page);
	$keyWordsArray = $linkBiz -> getKeywordArray();
	if(count($keyWordsArray)<1){
		header("Location:.");
		exit();
	}
	//对搜索到的记录进行处理
	for($i=0;$i<count($search_result);$i++){
		//截取文章内容
		$search_result[$i]['content'] = $format -> utf_substr($search_result[$i]['content'],$describe_fonts_num);
		//标记关键词
		for($j=0;$j<count($keyWordsArray);$j++){
			$search_result[$i]['title'] = $format -> mark($keyWordsArray[$j],$search_result[$i]['title']);
			$search_result[$i]['content'] = $format -> mark($keyWordsArray[$j],$search_result[$i]['content']);
		}
		$search_result[$i]['time'] = date("Y-m-d", $search_result[$i]['time']);
	}
	//总计搜索结果
	$search_total = $linkBiz->getSearchTotal();
	//总页码
	$page_total = ceil($search_total / $num_per_page);
	//模块
	$modBiz = new ModBiz();
	$mod_result = $modBiz -> getAll(0);
	//搜索相关词
	$historyBiz = new HistoryBiz();
	$likeWords = $historyBiz -> getLikeWords($keyWordsArray,10);
	if(!isset($_GET['web']) && !isset($_COOKIE["web"]) && $checkmobile->check()){
		$url = "?k=".$keyword."&t=".$_GET['t']."&m=".$_GET['m']."&p=";
		$page = new Page($page_current,$page_total,$url,'2',false,true);
		$page = $page->show();
		include(SEARCHTEMPLATEPATH.MOBILETEMPLATE."search_result.htm");
	}else{
		setcookie("web", "web", time()+3600);
		$url = "?k=".$keyword."&t=".$_GET['t']."&m=".$_GET['m']."&p=";
		$page = new Page($page_current,$page_total,$url);
		$page = $page->show();
		include(SEARCHTEMPLATEPATH.TEMPLATE."search_result.htm");
	}
}
function setHistory($keyword){
		setcookie("s_history[]",$keyword, time()+3600*24*365);
}
?>
